﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

using FlatRedBall;
using FlatRedBall.Graphics;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Input;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

using FlatRedBall.Graphics.Animation;

using ROMAP.Utilities;



namespace ROMAP.Entities.Enemies
{
    public class SentryEnemy : Enemy
    {
        private Sprite sprite;
        private float range;
        private bool deployed;
        private bool shotDirection; //right = false, left = true
        private float shotDelay; //in tenths of seconds
        private double initTime;
        private bool fired;
        public List<Bullet> bulletPool;

        public SentryEnemy(int x, int y, bool shotDirection)
        {
            TilePosition = new Vector2i(x, y);
            this.shotDirection = shotDirection;
            Initialize(true);
        }

        protected virtual void Initialize(bool addToManagers)
        {
            base.Initialize();

            sprite = SpriteManager.AddSprite(@"Content/Obstacles/rock");

            deployed = false;
            shotDelay = 10.0f;
            range = 15.0f;
            initTime = TimeManager.CurrentTime;
            fired = false;

            sprite.AttachTo(this, false);
            sprite.PixelScale();
            sprite.ScaleX *= 0.5f;
            sprite.ScaleY *= 0.5f;

            sprite.RelativeZ = 0.5f;

            Collision = ShapeManager.AddAxisAlignedRectangle();
            Collision.AttachTo(this, false);
            Collision.ScaleX = sprite.ScaleX;
            Collision.ScaleY = sprite.ScaleY;
            Collision.Visible = false;

            bulletPool = new List<Bullet>();
            for (int i = 0; i < 20; i++)
            {
                Bullet newBullet = new Bullet("Global");
                if (shotDirection)
                {
                    newBullet.Speed = -3.0f;
                }
                else
                {
                    newBullet.Speed = 3.0f;
                }
                newBullet.Range = range;
                newBullet.AttachTo(this, false);
                newBullet.xstart = newBullet.RelativeX;
                newBullet.ystart = newBullet.RelativeY;
                bulletPool.Add(newBullet);
            }
        }

        public override void Activity()
        {
            float temp = 5.0f;// EntityManager.EnemyDistance(this);
            if (!deployed && temp < range)
            {
                deployed = true;
                initTime = TimeManager.CurrentTime;
                this.Y = this.ystart + 0.25f;
            }
            else if (deployed && temp > range)
            {
                deployed = false;
                this.Y = this.ystart;
            }

            if (deployed)
            {

                if ((int)((TimeManager.CurrentTime - initTime) * 10) % (int)(shotDelay) == 0)
                {
                    if (!fired)
                    {
                        fired = true;
                        bulletPool.First(a => !a.Active).Activate();
                    }
                }
                else
                {
                    fired = false;
                }
            }

            foreach (Bullet e in bulletPool)
            {
                e.Activity();
            }
        }
    }
}
